package work.linruchang.util;

import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.lang.func.LambdaUtil;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReflectUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 增强的枚举工具类
 *
 * @author LinRuChang
 * @version 1.0
 * @date 2022/09/22
 * @since 1.8
 **/
public class EnhanceEnumUtil extends EnumUtil {

    /**
     * 判断某个字段值是否存在枚举中
     *
     * @param condition 条件字段
     * @param value       枚举属性值
     * @return 是否存在
     */
    public static <E extends Enum<E>, C> boolean contains(Func1<E, C> condition, C value) {
        return getBy(condition,value) != null;
    }

    /**
     * 判断目标枚举（targetEnum）的某个字段（condition）的值是否是入参value
     *
     * @param targetEnum 目标枚举
     * @param condition 条件字段
     * @param value       枚举属性值
     * @return 是否存在
     */
    public static <E extends Enum<E>, C> boolean contains(E targetEnum, Func1<E, C> condition, C value) {
        if(ObjectUtil.isAllNotEmpty(targetEnum,condition)) {
            C targetEnumFieldValue = (C) ReflectUtil.getFieldValue(targetEnum, LambdaUtil.getFieldName(condition));
            return ObjectUtil.equal(targetEnumFieldValue,value);
        }
        return false;
    }




    @Getter
    @AllArgsConstructor
    public static enum TestEnum {
        First("001","测试1"),
        Second("002","测试2");

        String code;
        String desc;
    }
    public static void main(String[] args) {
        boolean contains = EnhanceEnumUtil.contains(TestEnum::getCode, "001");
        Console.log(contains);
        contains = EnhanceEnumUtil.contains(TestEnum::getCode, "003");
        Console.log(contains);

        Console.log("\n======================");

        contains = EnhanceEnumUtil.contains(TestEnum.First, TestEnum::getDesc, "测试1");
        Console.log(contains);
        contains = EnhanceEnumUtil.contains(TestEnum.First, TestEnum::getDesc, "测试2");
        Console.log(contains);
    }

}
